<template>
  <div class="poster-box">
    <div class="poster-share">
      <div class="img-box">
        <van-swipe @change="onChange" :initial-swipe="current" :loop="false">
          <van-swipe-item v-for="(item,index) in posterList" :key="index">
            <img :src="item" alt="" srcset="">
          </van-swipe-item>
          <div class="custom-indicator" slot="indicator">
            {{ current + 1 }}/{{posterList.length}}
          </div>
        </van-swipe>
      </div>
      <div class="action-btn" @click="goSharePoster">分享海报</div>
    </div>
  </div>
</template>

<script>
// import marketService from 'SERVICE/marketService'
import sourceService from 'SERVICE/sourceService'
export default {
  data () {
    return {
      linkerId: '',
      posterList: [],
      current: 0,
    }
  },
  created () {
    this.linkerId = this.$route.query.linkerId
    this.current = +this.$route.query.current || 0
    this.getPosterList()
  },
  methods: {
    // 获取海报
    getPosterList () {
      sourceService
        .getAppliedMaterialList({
          linkerId: this.linkerId,
          type: 2,
          current: 1,
          size: 100
        })
        .then((res) => {
          if(res.records&&res.records.length){
            res.records.forEach(item => {
              this.posterList.push(item.content)
            })
          }
        }).catch()
    },
    // 预览图片切换
    onChange (index) {
      this.current  = index 
    },
    // 生成分享海报页面
    goSharePoster () {
      let imgUrl = this.posterList[this.current]
      this.$router.push({path: '/source/poster/share', query: {url: imgUrl, linkerId: this.linkerId}})
    }
  }
}
</script>

<style lang="less" scoped>
.poster-box{
  .img-box{
    width: 335px;
    height: 443px;
    margin: 20px 20px 32px;
    overflow: hidden;
    position: relative;
    background-color: #fff;
    .van-swipe{
      height: 100%;
      width: 100%;
      img{
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
    .custom-indicator {
      position: absolute;
      right: 8px;
      bottom: 8px;
      padding: 4px 12px;
      font-size: 12px;
      color: #fff;
      border-radius:10px;
      background: rgba(0, 0, 0, 0.5);
    }
  }
  .action-btn{
    margin: 0 20px;
    height:44px;
    background:rgba(0,122,230,1);
    border-radius:6px;
    font-size: 16px;
    text-align: center;
    line-height: 44px;
    color: #fff;
  }
}
</style>
